﻿using System;
using System.Collections.Generic;
using System.Text;
using MyCadDll.MyObject;
using System.Data;

namespace MyCadDll
{
    public class Chart:Object
    {
        double d = 2;
        int m_rowCount;
        int m_colCount;
        //double offsetX;
        //public double OffsetX
        //{
        //    get { return offsetX; }
        //    set { offsetX = value; }
        //}
        //double offsetY;
        //public double OffsetY
        //{
        //    get { return offsetY; }
        //    set { offsetY = value; }
        //}
        public List<MyLine> LineList;
        public List<MyCadObject> RowLineList;
        public List<MyCadObject> ColLineList;
        public List<MyText> TextList;
       // public List<MyCadObject> CadObjectList;
        public Dictionary<int, int> RowIndexDic;
        public Dictionary<int, int> ColIndexDic;

        private int CompareMyCadObj_X(MyCadObject obj1, MyCadObject obj2)
        {
            return obj1.CompareObjTo_X(obj2);
        }

        private int CompareMyCadObj_Y(MyCadObject obj1, MyCadObject obj2)
        {
            return obj1.CompareObjTo_Y(obj2);
        }

        public Chart()
        {
            MyCad cad = new MyCad();
            cad.StartCad();
            cad.GetSelectSet();
            LineList = cad.m_listMyLine;
            TextList = cad.m_listMyText;
           // CadObjectList = cad.m_listCadObjec;
            RowLineList = new List<MyCadObject>();
            ColLineList = new List<MyCadObject>();
            RowIndexDic = new Dictionary<int, int>();
            ColIndexDic = new Dictionary<int, int>();
            SetRowColLineList();
            ConstructText();
        }

        /// <summary>
        /// 构造文字对应的左直线和顶直线
        /// </summary>
        private void ConstructText()
        {
            foreach (MyText text in TextList)
            {
                text.SetLine(this);
            }
            ColLineList.Sort(CompareMyCadObj_X);
            RowLineList.Sort(CompareMyCadObj_Y);
            SetRowColIndexDic();
            //m_colCount = ColIndexDic.Count;
            //m_rowCount = RowIndexDic.Count;
            foreach (MyText text in TextList)
            {
                text.SetRowColIndex(this);
            }
            TextList.Sort();
        }

        /// <summary>
        /// 设置行和列对应的线段列表
        /// </summary>
        private void SetRowColLineList()
        {
            foreach (MyLine line in LineList)
            {
                if(Math.Abs(line.P1.X-line.P2.X)<CfgData.BiasD)
                {
                    if (Math.Abs(line.P1.Y - line.P2.Y) < CfgData.IgnoreLen)
                        continue;
                    line.P2.X = line.P1.X;
                    ColLineList.Add(line);
                }
                else if (Math.Abs(line.P1.Y - line.P2.Y) < CfgData.BiasD)
                {
                    if (Math.Abs(line.P1.X - line.P2.X) < CfgData.IgnoreLen)
                        continue;
                    line.P2.Y = line.P1.Y;
                    RowLineList.Add(line);
                }
            }
            ColLineList.AddRange(TextList.ToArray());
            RowLineList.AddRange(TextList.ToArray());
            ColLineList.Sort(CompareMyCadObj_X);
            RowLineList.Sort(CompareMyCadObj_Y);
            //SetRowColIndexDic();
            //m_colCount = ColIndexDic.Count;
            //m_rowCount = RowIndexDic.Count;
        }

        /// <summary>
        /// 定义每条线对应的行或列的序号字典
        /// </summary>
        private void SetRowColIndexDic()
        {
            int c=0,r=0;
            MyLine line,line1,preLine=null;
            int n;
            for (int i = 0; i < ColLineList.Count; i++)
            {
                if (ColLineList[i] is MyLine)
                {
                    line = ColLineList[i] as MyLine;
                    n = (int)(line.P1.X );

                    if (ColIndexDic.ContainsKey(n) == false)
                    {
                        if (i == 0)
                        {
                            ColIndexDic.Add(n, c);
                            preLine=line;
                        }
                        else
                        {
                            if (Math.Abs(preLine.P1.X - line.P1.X) < CfgData.RowColBiasD) 
                            {
                                ColIndexDic.Add(n, c);
                            }
                            else
                            {
                                ColIndexDic.Add(n, ++c);
                            }
                            preLine = line;
                        }
                    }
                }
            }
            for (int i = 0; i < RowLineList.Count; i++)
            {
                if (RowLineList[i] is MyLine)
                {
                    line = RowLineList[i] as MyLine;
                    n = (int)(line.P1.Y);

                    if (RowIndexDic.ContainsKey(n) == false)
                    {
                        if (i == 0)
                        {
                            RowIndexDic.Add(n, r);
                            preLine = line;
                        }
                        else
                        {
                            if (Math.Abs(preLine.P1.Y - line.P1.Y)< CfgData.RowColBiasD)
                            {
                                RowIndexDic.Add(n, r);
                            }
                            else
                            {
                                RowIndexDic.Add(n, ++r);
                            }
                            preLine = line;
                        }
                    }
                }
            }
            m_rowCount = r+1;
            m_colCount = c+1;
        }

        /// <summary>
        /// 把textList中的文字转换为DataTable.
        /// </summary>
        /// <returns></returns>

        public  DataTable ConvertDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("列0" , typeof(int));
            for(int i=1;i<=m_colCount;i++)
            {
                dt.Columns.Add("列" + (i).ToString(), typeof(string));
            }
            for (int i = 0; i <= m_rowCount; i++)
            {
                dt.Rows.Add(dt.NewRow());
                dt.Rows[i][0] = i;
            }
            if (m_rowCount == 0)
                return dt;
            foreach (MyText text in TextList)
            {
                if (text.ICol >= -1 && text.IRow >= -1)
                {
                    dt.Rows[text.IRow][text.ICol+1] +=" "+text.Text;
                }
            }
            return dt;
        }
       
    }
}
